package com.dslplugin;

import com.android.build.gradle.AppExtension;
import com.android.build.gradle.BaseExtension;
import com.android.build.gradle.LibraryExtension;
import com.android.build.gradle.internal.api.BaseVariantImpl;

import org.gradle.api.DefaultTask;
import org.gradle.api.DomainObjectSet;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.tasks.TaskContainer;
import org.gradle.api.tasks.TaskDependency;
import org.jf.dexlib2.DexFileFactory;
import org.jf.dexlib2.Opcodes;
import org.jf.dexlib2.iface.DexFile;
import org.jf.dexlib2.iface.Method;
import org.jf.dexlib2.iface.MethodImplementation;
import org.jf.dexlib2.iface.MethodParameter;
import org.jf.dexlib2.iface.reference.MethodReference;
import org.jf.dexlib2.rewriter.DexRewriter;
import org.jf.dexlib2.rewriter.Rewriter;
import org.jf.dexlib2.rewriter.RewriterModule;
import org.jf.dexlib2.rewriter.Rewriters;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import javax.annotation.Nonnull;

public class TestExtensionPlugin implements Plugin<Project> {

    static final String TAG = "任务TestExtensionPlugin:";
    public static void println(Object o){
        System.out.println(TAG + o.toString());
    }

    @Override
    public void apply(Project project) {

        System.out.println("---------------apply TestExtensionPlugin--------------");
        println ("插件buildSrc");
        project.getExtensions().create("myExtension", TExtension.class);
        project.getExtensions().create("myAddress", TAddress.class);
        DefaultTask dslTask = project.getTasks().create("dslCreateTask", DefaultTask.class);
        dslTask.doLast(p -> {
            println("task直正执行咯");
            String myAddressStr = p.getProject().getExtensions().findByType(TAddress.class).toString();
            String myExtensionStr = p.getProject().getExtensions().findByType(TExtension.class).toString();
            println("myAddressStr="+myAddressStr+",myExtensionStr="+myExtensionStr);
        }).setGroup("dsltasks");


        boolean hasAppPlugin = project.getPlugins().hasPlugin("com.android.application");
        BaseExtension baseExtension;
        DomainObjectSet variants;
        if (hasAppPlugin) {
            AppExtension appExtension = (AppExtension) project.getProperties().get("android");
            variants = appExtension.getApplicationVariants();
        } else {
            LibraryExtension libraryExtension = (LibraryExtension) project.getProperties().get("android");
            variants = libraryExtension.getLibraryVariants();
        }

//        project.afterEvaluate(project1 -> {
//
//            TaskContainer container = project1.getTasks();
//            println("container="+container);
//
//            container.getByName("transformDexArchiveWithDexMergerForZroTestDebug").getInputs().getFiles().forEach(file1 -> {
//                println("--------transformDexArchiveWithDexMergerForZroTestDebug inputFile="+file1);
//            });
//
//            container.getByName("transformDexArchiveWithDexMergerForZroTestDebug").getOutputs().getFiles().forEach(file2 -> {
//                println("--------transformDexArchiveWithDexMergerForZroTestDebug outputFile="+file2);
//            });
//
//            variants.all(variant -> {
//                println("dslTask="+dslTask.getName());
//                println("变体variant="+variant);
//                BaseVariantImpl baseVariant = (BaseVariantImpl) variant;
//                Task findTask = project1.getTasks().findByName(dslTask.getName());
//                println("查找Task="+findTask.getName());
//                println("依赖Task="+baseVariant.getPreBuildProvider().get().getName());
//                Task targetProviderTask = baseVariant.getMergeResourcesProvider().get();
//                TaskDependency taskDependency = targetProviderTask.getTaskDependencies();
//                Set sets = taskDependency.getDependencies(targetProviderTask);
//                findTask.dependsOn(sets);
//                targetProviderTask.dependsOn(findTask);
//            });
//        });
    }
}
